package com.jzcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.jzcn.common.base.BaseServiceImpl;
import com.jzcn.common.dto.file.FileDTO;
import com.jzcn.common.file.FileNameProcessor;
import com.jzcn.common.file.NameGenerator;
import com.jzcn.common.file.RelativePathGenerator;
import com.jzcn.common.file.StoreFileProcessRunnable;
import com.jzcn.common.utils.BeanUtils;
import com.jzcn.domain.SysFile;
import com.jzcn.domain.SysLog;
import com.jzcn.mapper.SysFileMapper;
import com.jzcn.mapper.SysLogMapper;
import com.jzcn.service.SysFileService;
import com.jzcn.service.SysLogService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * @author 梦里小白龙
 * @version 0.0.1
 * @date 2019/12/5 13:59
 */
@Service
public class SysFileServiceImpl extends BaseServiceImpl<SysFileMapper, SysFile> implements SysFileService {

    @Value("${sys.file.path}")
    private String sitePrefix;


    @Value("${sys.file.uploadPath}")
    private String fileStorePrefix;


    @Override
    public FileDTO storeFile(byte[] bytes, String fileFullName,Integer fileType, Integer userId) {
        return BeanUtils.convert(this.save(bytes, fileFullName,fileType, userId), FileDTO.class);
    }

    @Override
    public String getUrl(Integer fileId) {
        SysFile sysFile = super.getById(fileId);
        return generateRelativePath(sysFile);
    }

    @Override
    public FileDTO getById(Integer id) {
        return BeanUtils.convert(super.get(id), FileDTO.class);
    }

    @Override
    public String getUrl(FileDTO sysFileBo) {
        return generateRelativePath(BeanUtils.convert(sysFileBo, SysFile.class));
    }

    @Override
    public String getLocalPath(FileDTO sysFileBo) {
        return fileStorePrefix + sysFileBo.getStorePath();
    }


    protected SysFile save(byte[] byteFile, String fileFullName,Integer fileType,Integer userId) {
        SysFile record = new SysFile();
        record.setCreateTime(new Date());
        FileNameProcessor.FileName fileName = FileNameProcessor.process(fileFullName);
        record.setFileName(fileName.getName());
        record.setFileSuffix(fileName.getType());
        String storeFileName = NameGenerator.generate();
        record.setStoreFileName(storeFileName);
        String relationPath = RelativePathGenerator.generate(storeFileName, record.getFileSuffix(), record.getCreateTime());
        record.setStorePath(relationPath);
        record.setViewPath(generateRelativePath(record));
        record.setCreateUserId(userId);
        record.setFileType(fileType);
        this.save(record);

        StoreFileProcessRunnable storeFileProcessRunnable = new StoreFileProcessRunnable(byteFile,
        		record.getStoreFileName(), record.getFileSuffix(),
        		record.getCreateTime(), record.getId(), this.fileStorePrefix);
        storeFileProcessRunnable.run();

        return record;
    }

    protected String generateRelativePath(SysFile tFileRecord) {
        if (tFileRecord != null) {
            return this.sitePrefix == null ? "" : this.sitePrefix + tFileRecord.getStorePath();
        }
        return null;
    }

    @Override
    public Boolean updateFile(FileDTO fileBo) {
        SysFile sysFile = BeanUtils.convert(fileBo, SysFile.class);
        return this.updateById(sysFile);
    }

    @Override
    public Boolean saveBatchFile(List<FileDTO> sysFileBos) {
        List<SysFile> fileData = BeanUtils.convertList(sysFileBos,SysFile.class);
        return this.saveBatch(fileData);
    }

}
